package 力扣日常刷题.木23一月.第08天308;

/**
 * @author 帅小伙
 * @date 2022/3/8
 * @description
 */
public class Demo16在排序数组中查找数字I {


    public int search(int[] nums, int target) {
        if (nums.length == 0) return 0;
        int l = 0, r = nums.length - 1;

        // 先找左边界
        while (l < r) {
            int mid = (l + r) >> 1;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        if(nums[l] != target) return 0;
        int k = l;
        r = nums.length - 1;
        //找右边界
        while(l < r) {
            int mid = (l + r + 1) >> 1;
            if(nums[mid] <= target) l = mid;
            else r = mid - 1;
        }
        return r - k + 1;
    }
}
